<template>
  <el-dialog :visible.sync="_show" title="关联预售活动" destroy-on-close width="60%" @open="onOpen()">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-row :span="24">
        <el-col :span="24">
          <el-form-item label="活动名称">
            <el-input v-model="formInline.activityName" style="width:200px" />
          </el-form-item>
          <el-form-item label="活动编码">
            <el-input v-model="formInline.activityCode" style="width:200px" />
          </el-form-item>
          <el-form-item>
            <el-button @click="search()" type="primary" size="mini">查询</el-button>
            <el-button @click="kong()" type="primary" size="mini">清空</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-table
      class="table-main-cls"
      :data="tableData"
      border
      style="width: 100%"
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
      @current-change="handleSelectionChange"
      highlight-current-row
      height="500"
    >
      <el-table-column type="index" label="序号" width="60" align="center" />
      <el-table-column prop="activityName" label="活动名称" align="center" />
      <el-table-column prop="activityCode" label="活动编码" align="center" />
      <el-table-column prop="activityType" label="活动类型" align="center">
        <template slot-scope="scope">
          {{ scope.row.activityType | activityTypeDesc }}
        </template>
      </el-table-column>
      <el-table-column prop="activityStatus" label="活动有效期" align="center">
        <template slot-scope="scope">
          {{ scope.row.beginDate }} 至 {{ scope.row.endDate }}
        </template>
      </el-table-column>
    </el-table>

    <div class="block" style="margin-top:22px;overflow:hidden">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 30, 50, 100, 150, 200]" :current-page="listQuery.current"
        :page-size="listQuery.size" layout="total, sizes, prev, pager, next, jumper"
        :total="total"></el-pagination>
    </div>

    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { post } from '../../../utils/request';

export default {
  model: {
    prop: 'dialogVisible',
    event: 'update:dialogVisible'
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formInline: {
        activityName: "",
        activityCode: "",
      },
      listQuery: {
        current: 1,
        size: 10
      },
      total: 0,
      tableData: [],
      currentRow: null,
    }
  },

  computed: {
    _show: {
      get() {
        return this.dialogVisible;
      },
      set(val) {
        this.$emit('update:dialogVisible', val)
      }
    }
  },

  filters: {
    activityTypeDesc(val) {
      return val == 1 ? '定金预售' : '全款预售';
    }
  },

  methods: {
    onOpen() {
      this.listQuery = this.$options.data().listQuery;
      this.search();
    },
    search() {
      post('/api/flashsale-service/v1/presale/queryPreSaleActList', {
        condition: this.formInline,
        ...this.listQuery
      }).then(res => {
        if (res.code == 1) {
          this.tableData = res.records;
          this.total = res.total;
          this.listQuery.current = res.current;
          this.listQuery.size = res.size;
        }
      })
    },
    kong() {
      this.formInline = this.$options.data().formInline;
    },

    handleSizeChange(val) {
      this.listQuery.size = val;
      this.search();
    },
    handleCurrentChange(val) {
      this.listQuery.current = val;
      this.search();
    },

    handleSelectionChange(val) {
      this.currentRow = val;
    },

    onSubmit() {
      this.$emit('change', this.currentRow);
      this._show = false;
    }
  }
}
</script>

<style lang="scss" scoped>
.table-main-cls {
  /deep/ .el-table__row.current-row {
    color: red;
  }
}
</style>